package com.zjx.springcloud.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.security.token.SSOToken;
import com.zjx.springcloud.constant.HttpCode;
import com.zjx.springcloud.entities.Dept;
import com.zjx.springcloud.entities.ResultVO;
import com.zjx.springcloud.redis.RedisUtil;
import com.zjx.springcloud.service.DeptService;
import com.zjx.springcloud.utils.LanguageUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.apache.solr.client.solrj.SolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * spring boot demo 接口
 */
@RestController
@RequestMapping("/home")
public class HomeIndexController {
      @Resource
      private RedisUtil redisUtil;

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected HttpServletResponse response;

    /**

     * @param
     * @return
     */
    @RequestMapping(value = "/login",method = {RequestMethod.POST,RequestMethod.GET})
    public ResultVO login(
         User user
                                           ) {
        JSONObject res_jb = new JSONObject();

       System.out.println("*********");
        SSOHelper
                .setCookie(request, response, SSOToken.create()
                        .setIp(request).setId(1000)
                        .setIssuer("kisso"), false);

        SSOHelper.kickLogin(11);

            //  从SecurityUtils里边创建一个 subject
         Subject subject=SecurityUtils.getSubject();

      UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassword());


        try {
            // 在认证提交前准备 token（令牌）

            subject.login(token);
        } catch (UnknownAccountException uae) {
         //   return "未知账户";
            return    new ResultVO(HttpCode.REQUEST_ERROR_CODE, "未知账户");
        } catch (IncorrectCredentialsException ice) {
         //   return "密码不正确";
            return    new ResultVO(HttpCode.REQUEST_ERROR_CODE, "密码不正确");

        } catch (LockedAccountException lae) {
         //   return "账户已锁定";
            return    new ResultVO(HttpCode.REQUEST_ERROR_CODE, "账户已锁定");

        } catch (ExcessiveAttemptsException eae) {
         //   return "用户名或密码错误次数过多";
            return    new ResultVO(HttpCode.REQUEST_ERROR_CODE, "用户名或密码错误次数过多");
        } catch (AuthenticationException ae) {
         //   return "用户名或密码不正确！";
            return    new ResultVO(HttpCode.REQUEST_ERROR_CODE, "用户名或密码不正确");

        }
        if (subject.isAuthenticated()) {
         //   return "登录成功";
            return    new ResultVO(HttpCode.REQUEST_SUCCESS_CODE, "登录成功");

        } else {
            token.clear();
         return    new ResultVO(HttpCode.REQUEST_ERROR_CODE,"登录失败");

         //   return "登录失败";
        }



    }





}
